/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-11-12 下午12:31:46
 */
package com.absir.appserv.system.asset;

import java.util.Map;

import com.absir.appserv.system.bean.JOpenUser;
import com.absir.appserv.system.bean.JPayTrade;
import com.absir.appserv.system.bean.proxy.JiUserBase;
import com.absir.appserv.system.helper.HelperJson;
import com.absir.appserv.system.service.BeanService;
import com.absir.appserv.system.service.IPlatformIdentity;
import com.absir.appserv.system.service.MiguService;
import com.absir.appserv.system.service.SecurityService;
import com.absir.appserv.system.service.utils.PayUtils;
import com.absir.core.dyna.DynaBinder;
import com.absir.core.kernel.KernelObject;
import com.absir.core.kernel.KernelString;
import com.absir.server.in.Input;
import com.absir.server.value.Binder;
import com.absir.server.value.Body;
import com.absir.server.value.Server;

/**
 * @author absir
 * 
 */
@Server
public class Asset_migu extends AssetServer {

	/**
	 * @author absir
	 *
	 */
	public static class LoginData {
		public String userId;// M String 10 用户伪码
		public String key;// M String 24 用户登录网游的事务ID。
		public String cpId;// M String 6 合作方ID
		public String cpServiceId;// M String 12 计费代码
		public String channelId;// M String 8 8位渠道代码
		public String p;// O String 16 透传参数（即2.3.2接口设置的登录透传参数）
		public String region;// O String 30 用户归属地信息
		public String Ua;// M String
							// 用户手机型号（短UA形式），如用户手机型号为MOT-MOTORAZRV8_CMCC/1.0
							// linuxOS/2.6.10
							// Release/06.30.2007.Browser/Opera8.50
							// Profile/MIDP-2.0Configuration/CLDC-1.1software/R601_G_90.44.2BR则平台传递ua为MOT-MOTORAZRV8
	}

	/**
	 * @param loginData
	 * @return
	 */
	@Body
	public String login(@Binder LoginData loginData) {
		if (!KernelString.isEmpty(loginData.userId)) {
			JOpenUser openUser = new JOpenUser();
			openUser.setId(MiguService.PLAT_FORM_NAME + "@" + loginData.userId);
			openUser.setParam(loginData.p);
			BeanService.ME.merge(openUser);
			return "0";
		}

		return "1";
	}

	/**
	 * @param userId
	 * @return
	 */
	@Body
	public String session(String userId, String key) {
		JOpenUser openUser = BeanService.ME.get(JOpenUser.class, MiguService.PLAT_FORM_NAME + "@" + userId);
		if (MiguService.MIGU_CONFIGURE.isTest() || (openUser != null && key.equals(openUser.getParam()))) {
			JiUserBase userBase = SecurityService.ME.openUserBase(userId, null, MiguService.PLAT_FORM_NAME);
			if (userBase != null) {
				return IPlatformIdentity.PLATFORM_IDENTITY.loginPlatform(userBase);
			}
		}

		return "failed";
	}

	public static class NotifyData {
		public String userId;// M String 10 用户伪码
		public String contentId;// M String 12 计费代码
		public String consumeCode;// M String 12 道具计费代码
		public String cpId;// M String 6 合作代码
		public String hRet;// M String 1 平台计费结果（状态码外码）0-成功 其他-失败
		public String status;// M String 4 返回状态（内码），详见5.2章节
		public String versionId;// M String 5 版本号2_0_0, 统一填写2_0_0
		public String cpparam;// O String 16 CP透传参数，同2.7.2章节中的付费接口参数一致。
		public String packageID;// O String 12 套餐包ID(非局数据ID)
	}

	/**
	 * @param xml
	 * @param input
	 * @return
	 */
	@Body
	public String notify(@Body String xml, Input input) {
		Map<String, Object> map = HelperJson.xmlToMap(xml);
		if (KernelObject.equals(map.get("status"), "1800")) {
			NotifyData notifyData = DynaBinder.to(map, NotifyData.class);
			String tradeId = notifyData.cpparam;
			if (!KernelString.isEmpty(tradeId)) {
				JPayTrade payTrade = BeanService.ME.get(JPayTrade.class, tradeId);
				if (payTrade != null) {
					payTrade.setPlatform(MiguService.PLAT_FORM_NAME);
					PayUtils.proccessDone(payTrade);
				}
			}
		}

		return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response><hRet>0</hRet><message>ok</message></response>";
	}
}
